/**
 * Copyright (c) 2012, Camelot and/or its affiliates. All rights reserved.
 * Camelot PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.camelot.ccr.system.dao.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import com.camelot.ccr.global.dao.genericimpl.GenericHibernateDaoImpl;
import com.camelot.ccr.global.util.Pager;
import com.camelot.ccr.global.util.Utility;
import com.camelot.ccr.system.controller.DriverController;
import com.camelot.ccr.system.dao.PictureDao;
import com.camelot.ccr.system.persistence.Picture;

/**
 * <pre>
 * 图片
 * </pre>
 * @author Liu Jun
 * @version V0.1, 2014-5-20 下午10:30:05
 */
@Repository
public class PictureDaoImpl extends GenericHibernateDaoImpl<Picture, String> implements PictureDao {

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.PictureDao#findPageList(int, int, com.camelot.ccr.system.persistence.Picture)
	 */
	@Override
	public Pager<Picture> findPageList(int curpage, int pageSize, Picture example) {
		StringBuilder sb = new StringBuilder("from Picture p where 1=1  ");
		List<Object> parameList = new ArrayList<Object>();
		if(Utility.isNotEmpty(example.getBusinessId())){
			sb.append(" and p.businessId = ? ");
			parameList.add(example.getBusinessId());
		}
		if(Utility.isNotEmpty(example.getMoudleType())){
			sb.append(" and p.moudleType = ? ");
			parameList.add(example.getMoudleType());
		}
		return (Pager<Picture>) queryListPageByHql(sb.toString(), curpage, pageSize, parameList);
	}

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.PictureDao#findPictureSet()
	 
	@Override
	public List<Picture> findPictureSet() {
		List<Picture> list = getAll();
		int max = list.size();
		int min = 0;
		if(max==0){
			return null;
		}
		Random random = new Random();
		for
        int s = random.nextInt(max)%(max-min+1) + min;
		return null;
	}*/
	
	public static void main(String[] args) {
		int max = 0;
		int min = 0;
		
		Random random = new Random();

        int s = random.nextInt(max)%(max-min+1) + min;
        System.out.println(s);
	}

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.PictureDao#savePicture(com.camelot.ccr.system.persistence.Picture, org.springframework.web.multipart.MultipartFile)
	 */
	@Override
	public void savePicture(Picture picture, MultipartFile file) throws IllegalStateException, IOException {
		String imgUrl = picture.getImgUrl();
		String path = Utility.uploadFile(file, imgUrl,DriverController.driverZysyPath);
		picture.setImgUrl(path);
		save(picture);
	}
	
}
